 package com.sky.controller;

import com.sky.constant.MessageConstant;
import com.sky.dto.common.COSParams;
import com.sky.entity.FileList;
import com.sky.enumeration.StatusDescriptionManager;
import com.sky.mapper.FileListMapper;
import com.sky.result.Result;
import com.sky.utils.QcloudUtil;
import com.tencent.cloud.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("api/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
    @Autowired
    private QcloudUtil qcloudUtil;
    @Autowired
    private FileListMapper fileListMapper;
    /**
     * 获取临时密钥
     * @param cosParams
     * @return
     */
    @PostMapping("/SDK")
    public Result<Response> SKD(@RequestBody COSParams cosParams) {
        log.info("接收的参数{}",cosParams);
        Response tmpSecret = qcloudUtil.getTmpSecret(cosParams.getValue());
        return Result.success(tmpSecret, MessageConstant.OK_MESSAGE);
    }
    /**
     * 获取临时签名 - get
     */
    @PostMapping("/sign")
    public Result<String> getSign(@RequestBody COSParams cosParams) throws Exception {
        String sign = qcloudUtil.getSignature(cosParams.getValue(),cosParams.getKey());
        return Result.success(sign, MessageConstant.OK_MESSAGE);
    }
    /**
     * 获取签名头信息 - fetch使用
     */
    @PostMapping("/signHeader")
    public Result<Map<String,String>> getSignHeader(@RequestBody COSParams cosParams) {
        Map<String,String> signHeader = qcloudUtil.getSignatureForGetAndHead(cosParams.getValue(),cosParams.getKey());
        return Result.success(signHeader, MessageConstant.OK_MESSAGE);
    }

    /**
     * 获取签名头信息 - head使用
     * @param cosParams
     * @return
     */
    @PostMapping ("/signHead")
    public Result<String> getSignHead(@RequestBody COSParams cosParams) throws Exception {
        String sign = qcloudUtil.headSignature(cosParams.getValue(),cosParams.getKey());
        return Result.success(sign, MessageConstant.OK_MESSAGE);
    }
    /**
     * 删除文件 - cos
     */
    @DeleteMapping("/deleteFileCos")
    public Result<String> deleteFileCos(@RequestBody COSParams cosParams) {
        // 删除cos
        qcloudUtil.deleteObject(cosParams.getValue(),cosParams.getKey());
        // 删除本地数据库记录
        fileListMapper.deleteByid(cosParams.getUid());
        return Result.success(null, MessageConstant.OK_MESSAGE);
    }
}
